using Ease.EaseNode.Editor.NodeView;
using Ease.EaseNode.Runtime;
using Ease.EaseNode.Runtime.Param;
using Ease.TestEaseNode.Runtime;
using Ease.TestEaseNode.Runtime.Param;
using Unity.VisualScripting;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;

namespace Ease.TestEaseNode.Editor
{
    public sealed class TestEaseNode : BaseEaseNode
    {
        private TextField textField;
        private TestData testData;

        public TestEaseNode(IEaseNodeDataState state) : base(state)
        {
            title = state == null ? "TestEaseNode" : $"TestEaseNode_{state.Key}";
            contentContainer.style.backgroundColor = Color.gray;
            textField = new TextField();
            textField.multiline = true;
            textField.RegisterCallback<ChangeEvent<string>>(OnTextFieldChange);
            contentContainer.Add(textField);
            testData = State.Data as TestData;
            if (testData != null)
            {
                title = $"{testData.DataDes}_{testData.id}";
                textField.SetValueWithoutNotify(testData?.content);
            }
        }

        public override void OnNodeRemove()
        {
            base.OnNodeRemove();
            textField.UnregisterCallback<ChangeEvent<string>>(OnTextFieldChange);
            textField = null;
            testData = null;
        }

        private void OnTextFieldChange(ChangeEvent<string> a)
        {
            if (testData != null)
                testData.content = a.newValue;
        }

        protected override void OnStateChange(BaseChangeParam param)
        {
            Debug.Log("OnStateChange");

            if (param.Key == typeof(StateChangeParam).Name)
            {
                var param2 = param as StateChangeParam;
                switch (param2.newState)
                {
                    case NodeState.None:
                        contentContainer.style.backgroundColor = Color.gray;
                        break;
                    case NodeState.Start:
                        contentContainer.style.backgroundColor = Color.green;
                        break;
                    case NodeState.Running:
                        contentContainer.style.backgroundColor = Color.green;
                        break;
                    case NodeState.Finish:
                        contentContainer.style.backgroundColor = Color.red;
                        break;
                }
            }
            else if (param.Key == typeof(StringChangeParam).Name)
            {
                var param2 = param as StringChangeParam;
                textField.SetValueWithoutNotify(param2.value);
            }
            else if (param.Key == typeof(SettingChangeParam).Name)
            {
                var param2 = param as SettingChangeParam;
                contentContainer.style.backgroundColor = param2.NodeBGColor;
            }
        }
    }
}